/**
@author		Jong Min, Lee
@date		  02/12/2014
@breif		study view layout property

copyright(c) Tae Young Soft. All right reserved. 2014
*/
#include "stdafx.h"
#include "Taurus_Viewer/StudyViewLayoutProperty.h"

#include "Taurus_Common/IAppGlobalInstanceSet.h"
#include "Taurus_Common/MonitorAdmin.h"
#include "Taurus_Viewer/VGlobalInstanceSet.h"

const C2DLayout CStudyViewLayoutProperty::theDefault(1,1);

CStudyViewLayoutProperty::CStudyViewLayoutProperty()
{
}

CStudyViewLayoutProperty::~CStudyViewLayoutProperty()
{
}

DWORD CStudyViewLayoutProperty::doConfigure()
{
  m_theLayouts.clear();

  IAppGlobalInstanceSet* theVGSet = CVGlobalInstanceSet::getInstance()->getAppGlobalInstanceSet();
  CMonitorAdmin* theMAdmin = theVGSet->getMonitorAdmin();

  unsigned int numMonitors = theMAdmin->getNumVisibleMonitors();
  try
  {
    m_theLayouts.reserve(numMonitors);
  }
  catch(...)
  {
    return ERROR_NOT_ENOUGH_MEMORY;
  }

  for(unsigned int i = 0; i < numMonitors; ++i)
    m_theLayouts.push_back(theDefault);

  try
  {
    m_theAvailableLayouts.reserve(MAX_NUM_STUDY_VIEW_ROWS * MAX_NUM_STUDY_VIEW_COLS);
  }
  catch(...)
  {
    m_theLayouts.clear();
    return ERROR_NOT_ENOUGH_MEMORY;
  }

  C2DLayout available;
  for(short row = 1; row <= MAX_NUM_STUDY_VIEW_ROWS; ++row)
  {
    available.theRows = row;
    for(short col = 1; col <= MAX_NUM_STUDY_VIEW_COLS; ++col)
    {
      available.theCols = col;
      m_theAvailableLayouts.push_back(available);
    }
  }
      

  return ERROR_SUCCESS;
}

const C2DLayout* CStudyViewLayoutProperty::getLayout(const unsigned int index) const
{
  if(index >= m_theLayouts.size()) return NULL;

  return &(m_theLayouts[index]);
}

DWORD CStudyViewLayoutProperty::setLayout(const unsigned index, const C2DLayout& layout)
{
  if(index >= m_theLayouts.size()) return ERROR_INVALID_PARAMETER;

  m_theLayouts[index] = layout;
  return ERROR_SUCCESS;
}

const C2DLayout* CStudyViewLayoutProperty::getAvailableLayout(const unsigned int index) const
{
  if(index >= m_theAvailableLayouts.size()) return NULL;

  return &(m_theAvailableLayouts[index]);
}

BOOL CStudyViewLayoutProperty::isAvailable(const C2DLayout& layout) const
{
  return (layout.theRows > 0 && layout.theRows <= MAX_NUM_STUDY_VIEW_ROWS &&
          layout.theCols > 0 && layout.theCols <= MAX_NUM_STUDY_VIEW_COLS);
}